package com.xxx.question.controller;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.xxx.question.entity.QmsTypeEntity;
import com.xxx.question.service.QmsTypeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import com.xxx.question.entity.QmsQuestionEntity;
import com.xxx.question.service.QmsQuestionService;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author platov
 * @email platov@admin.com
 * @date 2024-09-17 13:06:56
 */
@RestController
@RequestMapping("/question/qmsquestion")
public class QmsQuestionController {
    @Resource
    private QmsQuestionService qmsQuestionService;

    @Resource
    private QmsTypeService qmsTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = qmsQuestionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		QmsQuestionEntity qmsQuestion = qmsQuestionService.getById(id);

        return R.ok().put("qmsQuestion", qmsQuestion);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody QmsQuestionEntity qmsQuestion){
		qmsQuestionService.save(qmsQuestion);

        return R.ok();
    }

    /**
     * 修改
     */
    //题目上传导入
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        Map result = qmsQuestionService.importExcel(file);
        return R.ok().put("result",result.get("result")).put("msg",result.get("msg")).put("num",result.get("num"));
    }

    //导出excel
    @GetMapping("/exportExcel")
    public void export(String tableName, HttpServletResponse response){
        System.out.println("导出excele");
        Workbook workbook = qmsQuestionService.exportExcel();

        if (workbook != null) {
            String fileName = "uxue_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/octet-stream;charset=GB2312");
            response.setCharacterEncoding("GB2312");
            OutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		qmsQuestionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    @RequestMapping("/countTypeQuestion")
    public R countTypeQuestion(){
        List<Map<String, Object>> mapList = qmsQuestionService.countTypeQuestion();
        for (Map<String, Object> map : mapList) {

            //根据分类id，读取对应的分类数据
            Long typeId = (Long) map.get("TYPE");
            QmsTypeEntity typeEntity = qmsTypeService.getById(typeId);
            //重新封装分类名称到map
            map.put("name",typeEntity.getType());
            //{{TYPE:xxx,num:xxx,name:xxx}}
        }
        return R.ok().put("mapList",mapList);
    }

}
